﻿using System.Collections.Generic;
using System.Data;
using System.Windows.Forms;
using Ycjw.PipeLine.Commons;

namespace Ycjw.PipeLine.Map.Basic.Results
{
    public class ResultWindowWithMultiDataGridView: ResultWindowWithDataGridView
    {
        public List<DataGridView> m_DataGridViews = new List<DataGridView>();
        public ResultWindowWithMultiDataGridView() : base()
        {
        }

        public override void ShowData(object data)
        {            
            Controls.Clear();
            Sunny.UI.UITabControl utc = new Sunny.UI.UITabControl() { Dock = DockStyle.Fill };
            Controls.Add(utc);
            if (!(data is List<DataTable>))
                base.ShowData(data);
            else
            {
                var dats = data as List<DataTable>;
                for(int i = 0;i < dats.Count; i++)
                {
                    Sunny.UI.UIPage up = new Sunny.UI.UIPage
                    {
                        Text = dats[i].TableName
                    };
                    var dgv = new Sunny.UI.UIDataGridView()
                    {
                        ShowEditingIcon = false,
                        AllowUserToAddRows = false,
                        Dock = DockStyle.Fill,
                        ReadOnly = true,
                        MultiSelect = false,
                        Name = "dgv"
                    };
                    dgv.CellMouseDoubleClick += DataGridView_CellMouseDoubleClick;

                    up.Controls.Add(dgv);
                    if (dats.Count == 1 || i < dats.Count - 1) up.Dock = DockStyle.Fill;
                    else
                    {
                        up.Dock = DockStyle.Bottom;
                        up.Height = this.Height / dats.Count;
                    }
                    utc.AddPage(up);
                    dgv.DataSource = dats[i];
                    SetDataGridViewStyle(dgv);
                    dgv.RowPostPaint += DataGridView_RowPostPaint;
                }
                CreateExportExcelButton();
                uiSymbolButtonExportToExcel.Click += UiSymbolButtonExportToExcel_Click; ;
            }
        }

        private void UiSymbolButtonExportToExcel_Click(object sender, System.EventArgs e)
        {
            var cs = this.Controls.Find("dgv", true);
            var dgvs = new List<DataGridView>();
            foreach(var dgv in cs)
            {
                if(dgv is DataGridView newDgv)
                {
                    dgvs.Add(newDgv);
                }
            }
            if (dgvs.Count == 0) return;

            if (dgvs.Count == 1)
            {
                var errMsg = Commons.ExportDgvToExcel.ExportExcel(string.Empty, dgvs[0], "雅黑", 14);
                if (!string.IsNullOrEmpty(errMsg))
                {
                    if (errMsg == Commons.ExportDgvToExcel.CANCELRESULTSTRING) return;
                    Sunny.UI.UINotifier.Show(errMsg, Sunny.UI.UINotifierType.ERROR, "错误", false, 1000);
                }
                else
                {
                    Sunny.UI.UINotifier.Show("导出成功", Sunny.UI.UINotifierType.OK, "消息", false, 1000);
                }
                return;
            }
            FolderBrowserDialog dilog = new FolderBrowserDialog
            {
                Description = "请选择文件夹"
            };
            if (dilog.ShowDialog() == DialogResult.OK)
            {
                var errMsg = ExportDgvToExcel.ExportExcel(dilog.SelectedPath,dgvs.ToArray(),"雅黑",14);
                errMsg = errMsg.Replace(System.Environment.NewLine,string.Empty);
                if (!string.IsNullOrEmpty(errMsg))
                {
                    Sunny.UI.UINotifier.Show(errMsg, Sunny.UI.UINotifierType.ERROR, "错误", false, 1000);
                }
                else
                {
                    Sunny.UI.UINotifier.Show("导出成功", Sunny.UI.UINotifierType.OK, "消息", false, 1000);
                }
            }
        }
    }
}
